package com.jxc.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Transactional;

import com.jxc.entity.TbEmployee;
import com.jxc.dao.UsersDao;

@Transactional
public class UsersDaoImpl implements UsersDao {
	@Resource
	private SessionFactory sessionFactory;
	private Log log = LogFactory.getLog(getClass());

	public List login(String name) {
		List query = null;
		try {
			String hql = "select tbe,tbem.tbRole.roleId,tbem.tbRole.name  from TbEmployee as tbe,TbEmployeeRole as tbem where userName='"
				+ name + "' and tbe.id=tbem.tbEmployee.id and tbe.flag!=-1";
			query = sessionFactory.getCurrentSession().createQuery(hql).list();
			log.info("有一个用户正尝试登陆!");
		} catch (HibernateException e) {
			e.printStackTrace();
		}
		return query;
	}

	public TbEmployee FindById(int id) {
		TbEmployee Employee = null;
		try {
			String hql = "from TbEmployee where id='" + id + "'";
			Employee = (TbEmployee) sessionFactory.getCurrentSession()
					.createQuery(hql).uniqueResult();
		} catch (HibernateException e) {
			e.printStackTrace();
		}
		return Employee;
	}

}
